package com.example.moderndashboard.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.moderndashboard.Adapter.TrendsAdapter;
import com.example.moderndashboard.Domain.TrendSDomain;
import com.example.moderndashboard.R;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView.Adapter adapterTrendsList;
    private RecyclerView recyclerViewTrends;
    private EditText searchEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化 RecyclerView
        initRecyclerView();

        // 初始化 BottomNavigation
        BottomNavigation();

        // 初始化搜索框
        initSearch();
    }

    private void BottomNavigation() {
        LinearLayout profileBtn = findViewById(R.id.pofileBtn);
        profileBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ProfileActivity.class));
            }
        });
    }

    private void initRecyclerView() {
        ArrayList<TrendSDomain> items = new ArrayList<>();

        items.add(new TrendSDomain("Future in AI What will tomorrow be like?", "The National", "trends"));
        items.add(new TrendSDomain("Important points in concluding a work contract", "Reuters", "trends2"));
        items.add(new TrendSDomain("Future in AI What will tomorrow be like?", "The National", "trends"));

        recyclerViewTrends = findViewById(R.id.view1);
        recyclerViewTrends.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        // 使用 `this` 作为 Context 传递给 `TrendsAdapter`
        adapterTrendsList = new TrendsAdapter(this, items);
        recyclerViewTrends.setAdapter(adapterTrendsList);
    }

    private void initSearch() {
        searchEditText = findViewById(R.id.editTextTextPersonName);
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 如果输入包含“天气”关键词，则跳转到 WeatherActivity
                if (s.toString().toLowerCase().contains("天气")) {
                    Intent intent = new Intent(MainActivity.this, WeatherActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }
}
